<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/rss/rss20.class.php, $Author:  $ */

/**
 * Class for basic RSS creation.
 *
 * @package core
 *   unknown
 * 2   Evgenij Grouk aka GloGen
 * @version $Id: rss20.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 */

class RSS {

    var $_aChannel;

    var $_aImage;

    var $_aItems;


	function RSS () {
		$this->_aItems = array();
	}

	/** Set channel information
	 * @param string $sTitle  channel's title
	 * @param string $sDescr  channel's descripton
	 * @param string $sLink   channel's link
	 * @param string $sAbout  channel's url (url of RSS)
	 * @param array  $aParams additional parameters for different modules:
	 *                       1 Syndication - 'sy:updatePeriod', 'sy:updateFrequency', 'sy:updateBase'
	 */
	function setChannel($sTitle, $sDescr, $sLink, $aParams=array()) {
        $this->_aChannel['title']  = $sTitle;
        $this->_aChannel['descr']  = $sDescr;
        $this->_aChannel['link']   = $sLink;
	    $this->_aChannel['params'] = $aParams;
	}

    /** Add items to feed. For imte format - see addItem().
     * @param array $aItems items info (array of info arrays)
     * @see addItem()
     */
	function addData($aItems) {
		foreach($aItems as $aItem) {
			array_push($this->_aItems, $aItem);
		}
	}

	/** Add single item to feed.
	 * @param array $aItem single item info:
	 *         1. title   - item title
	 *         2. description   - item description
	 *         3. link    - link for item full version
	 *         4. dc:...  - [optional] Dublin Core module params (see http://dublincore.org/documents/1999/07/02/dces/)
	 * @see addData()
	 */
	function addItem ($aItem) {
		array_push($this->_aItems, $aItem);
	}

	/** Get source XML-code for RSS.
	 * @return string XML-code
	 * @see send()
	 */
	function get($sEncoding='utf-8') {
		$doc =
    		'<?xml version="1.0" encoding="'.$sEncoding.'"?>'."\n".
		    '<rss version="2.0">'."\n\n";
		$doc .= $this->_getChannel();
		$doc .='</rss>';
		return $doc;
	}

	/** Send generated XML-code to output.
	 * @see get()
	 */
	function send()
	{
	    header('Content-type: text/xml');
	    echo $this->get();
	    exit();
	}

	/** Create channel part of code
	 * @return string source for channel part of RSS.
	 */
	function _getChannel() {
		// start channel info
		$sChannel =
			'  <channel>'."\n".
			'    <title>'.$this->_escape($this->_aChannel['title']).'</title> '."\n".
			'    <link>'.$this->_aChannel['link'].'</link> '."\n".
			'    <description>'.$this->_escape($this->_aChannel['descr']).'</description>'."\n";
		foreach ($this->_aChannel['params'] as $sParamName=>$sVal){
		    $sChannel .=
		    '    <'.$sParamName.'>'.$this->_escape($sVal).'</'.$sParamName.'>';
		}
		if ($this->_aItems){
            $sChannel .= $this->_getItems();
		}
		$sChannel .= '  </channel>'."\n";
		return $sChannel;
	}

	/** Create code for items
	 * @return string source  code for items
	 */
	function _getItems() {
		$sItems = "";
		foreach ($this->_aItems as $aItem) {
			$sItems .= '      <item>'."\n".
			           '        <title>'.$this->_escape($aItem['title'].
			                                '').'</title>'."\n".
                       '        <link>'.$aItem['link'].'</link>'."\n".
                       '        <description>'.$this->_escape($aItem['description']).'</description>'."\n";
            foreach($aItem as $k=>$v) //process additionla parameters
            {
                if (! in_array($k, array('link', 'title', 'description')))
                    $sItems .= '        <'.$k.'>'.$this->_escape($v).'</'.$k.'>'."\n";
            }
            $sItems .= '      </item>'."\n\n";
		}
		return $sItems;
	}

	/** Enclode string in <![CDATA[ ... ]]> if needed
	 * @param string $sStr source string
	 * @return string enclosed string
	 */
	function _escape($sStr)
	{
	    if ( (strpos($sStr,'>')!==false) ||
	         (strpos($sStr,'<')!==false) ||
	         (strpos($sStr,'"')!==false) )
    	    return '<![CDATA['.$sStr.']]>';
    	else
    	    return $sStr;
	}
}

?>